package ys.ewp.facade;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import ys.ewp.dto.NoticeDto;
import ys.ewp.po.Column;
import ys.ewp.po.Employee;
import ys.ewp.po.Notice;
import ys.ewp.service.ColumnService;
import ys.ewp.service.EmployeeService;
import ys.ewp.service.NoticeService;
import ys.ewp.tool.FormatDate;

@Component
public class NoticeFacade {
	
	@Resource
	private NoticeService noticeService;
	
	@Resource
	private EmployeeService employeeService;
	
	@Resource
	private ColumnService columnService;
	
	/**
	 * @功能 获取所有公告
	 * @return
	 */
	public List<NoticeDto> getNoticeList(){
		List<Notice> notList = noticeService.getList();
		return this.getDtoListByPoList(notList);
	}
	
	/**
	 * @功能 获取所属栏目下的公告
	 * @param colId 栏目ID
	 * @return
	 */
	public List<NoticeDto> getNoticeListByColId(Integer colId){
		List<Notice> notList = noticeService.getNoticeListByColId(colId);
		return this.getDtoListByPoList(notList);
	}
	
	/**
	 * @功能 获取系统公告栏目下的公告
	 * @return
	 */
	public List<NoticeDto> getNoticeListBySysCol(){
		Object[] params = {"系统公告"};
		List<Notice> notList = noticeService.getList("o.column.title = ?", params, null);
		return this.getDtoListByPoList(notList);
	}
	
	/**
	 * @功能 获取今日（最新）公告
	 * @return
	 */
	public List<NoticeDto> getTodayNoticeList(){
		String todayDate = FormatDate.formatDate(new Date());
		Object[] params = {todayDate};
		List<Notice> notList = noticeService.getList("o.formatDateCreated = ?", params, null);
		System.out.println(notList.get(0).getTitle());
		return this.getDtoListByPoList(notList);
	}
	
	/**
	 * @功能 插入公告数据
	 * @param notDto
	 * @return
	 */
	public NoticeDto insertNotice(NoticeDto notDto){
		System.out.println(notDto.getContent());
		Column col = columnService.getColumnByTitle(notDto.getColTitle());
		Employee emp = employeeService.get(notDto.getEmpId());
		Notice not = NoticeDto.getPoByDto(notDto);
		not.setColumn(col);
		not.setEmployee(emp);
		noticeService.save(not);
		Notice tmpNot = noticeService.getLast();
		NoticeDto tmpNotDto = NoticeDto.getDtoByPo(tmpNot);
		return tmpNotDto;
	}
	
	
	
	
	
	/**
	 * 	@功能 根据PO列表获取DTO列表
	 * @param notList 
	 * @return 
	 */
	private List<NoticeDto> getDtoListByPoList(List<Notice> notList){
		
		if(notList == null || notList.isEmpty()){
			return Collections.emptyList();
		}
		List<NoticeDto> notDtoList = new ArrayList<NoticeDto>();
		for(Notice not : notList){
			NoticeDto notDto = null;
			notDto = NoticeDto.getDtoByPo(not);
			
			notDtoList.add(notDto);
		}
		return notDtoList;
	}
	
}
